﻿package net.steam.easyjoboa.web.action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import net.steam.easyjoboa.biz.EmployeeBiz;
import net.steam.easyjoboa.biz.VoteBiz;
import net.steam.easyjoboa.biz.VoteDetailBiz;
import net.steam.easyjoboa.biz.VoteItemBiz;
import net.steam.easyjoboa.entity.Employee;
import net.steam.easyjoboa.entity.PageBean;
import net.steam.easyjoboa.entity.PageBeans;
import net.steam.easyjoboa.entity.Vote;
import net.steam.easyjoboa.entity.VoteDetail;
import net.steam.easyjoboa.entity.VoteItem;

public class VoteDetailAction {
	private VoteDetailBiz voteDetailBiz;
	private VoteItemBiz voteItemBiz;
	private EmployeeBiz employeeBiz;
	private VoteBiz voteBiz;
	private int id;
	private PageBeans pageBean;
	private int pageIndex=1;
	private int pageSize=5;
	private List<Employee> list;
	private List<VoteDetail> list2;
	private List<Integer> list3;
	private Vote vote;
	private VoteDetail voteDetail;
	private int userId;
	private int voteId;
	Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
	

	@Action(value="getListByVoteDetail",results={@Result(name="success",location="../../vote/voteDetail.jsp")})
	public String getEmployeeListByPage(){
		if(pageBean==null){
			pageBean = new PageBeans();
		}
		pageBean.setCurrentPage(pageIndex);
		if(pageSize<=0){
			pageSize=5;
		}
		pageBean.setPageSize(pageSize);
		int dep=employee.getPosition().getDepartment().getDepartmentId();
		list=employeeBiz.getEmployeesByDepartmentId(pageBean,dep);
		vote=voteBiz.getVote(id);
		return "success";
	}
	
	
	
	@Action(value="voteDetail",results={@Result(name="success",location="voteList",type="redirectAction"),
								@Result(name="error",location="../../vote/error.jsp")})
								
	/*
	 * 判断投票详细中是否有该员工,如果有,count++;否则添加新数据
	 * 
	 */
	public String vote(){
		Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
		List<VoteItem> list = voteItemBiz.getVoteItemsByUserId(employee.getUserId(),voteId);
		if(list.size()==0){			
			VoteItem voteItem = new VoteItem();
			voteItem.setEmployee(employee);
			Vote vote = new Vote();
			vote.setVoteId(voteId);
			voteItem.setVote(vote);
			voteItemBiz.addVoteItem(voteItem);
		
			VoteDetail voteDetail = new VoteDetail();
			Vote votes = new Vote(); 
			Employee employees = new Employee();
			List<VoteDetail> list2 = voteDetailBiz.getVoteDetail(employee.getUserId(),voteId);
			if(list2.size()==0){
				//添加
				votes.setVoteId(voteId);
				employees.setUserId(employee.getUserId());
				voteDetail.setVote(votes);
				voteDetail.setEmployee(employees);
				voteDetail.setVoteCount(1);
				voteDetailBiz.addVoteDetail(voteDetail);
				return "success";
			}else{
				//count++
				VoteDetail voteDetail2 = list2.get(0);
				int count = voteDetail2.getVoteCount();
				count++;
				voteDetail2.setVoteCount(count);
				voteDetailBiz.updateVoteDetail(voteDetail2);
				return "success";
			}				
		}else{
			return "error";
		}	
	}

	@Action(value="selvoteResult",results={
			@Result(name="success",location="../../vote/adminSelectResult.jsp")})
	public String voteResult(){

		
		list2 = voteDetailBiz.getVoteDetailsByVoteId(id);
		return "success";
	}
	
	

	public void setVoteDetailBiz(VoteDetailBiz voteDetailBiz) {
		this.voteDetailBiz = voteDetailBiz;
	}

	public void setEmployeeBiz(EmployeeBiz employeeBiz) {
		this.employeeBiz = employeeBiz;
	}
	
	
	public PageBeans getPageBean() {
		return pageBean;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setPageBean(PageBeans pageBean) {
		this.pageBean = pageBean;
	}


	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}


	public List<Employee> getList() {
		return list;
	}


	public void setList(List<Employee> list) {
		this.list = list;
	}


	public Vote getVote() {
		return vote;
	}


	public void setVote(Vote vote) {
		this.vote = vote;
	}


	public void setVoteBiz(VoteBiz voteBiz) {
		this.voteBiz = voteBiz;
	}

	public VoteDetail getVoteDetail() {
		return voteDetail;
	}

	public void setVoteDetail(VoteDetail voteDetail) {
		this.voteDetail = voteDetail;
	}

	public void setVoteItemBiz(VoteItemBiz voteItemBiz) {
		this.voteItemBiz = voteItemBiz;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getVoteId() {
		return voteId;
	}

	public void setVoteId(int voteId) {
		this.voteId = voteId;
	}


	public List<VoteDetail> getList2() {
		return list2;
	}


	public void setList2(List<VoteDetail> list2) {
		this.list2 = list2;
	}


	public List<Integer> getList3() {
		return list3;
	}


	public void setList3(List<Integer> list3) {
		this.list3 = list3;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
}
